<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/facicon.ico?v=5.1.4" color="#222">





  <meta name="keywords" content="Java,">





  <link rel="alternate" href="/atom.xml" title="Key" type="application/atom+xml">






<meta name="description" content="现在要求定义一个数组（整型数据）的操作类，这个类有如下几点要求：  数组的大小由类创建对象的时候动态决定； 可以通过类向数组中进行数据的保存，保存的时候需要考虑到数组的大小问题； 如果发现数组空间不足，则可以进行数组长度的动态扩充； 可以取得数组的全部数据（增加顺序）；  而后继续在这个基础上要求扩展两个派生类：  可以进行数组的排序操作； 可以进行数组的反转处理；  定义一个Array类 在定义">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 综合案例：数组操作">
<meta property="og:url" content="https://blog.qipo.net/2018/02/17/java1/index.html">
<meta property="og:site_name" content="Key">
<meta property="og:description" content="现在要求定义一个数组（整型数据）的操作类，这个类有如下几点要求：  数组的大小由类创建对象的时候动态决定； 可以通过类向数组中进行数据的保存，保存的时候需要考虑到数组的大小问题； 如果发现数组空间不足，则可以进行数组长度的动态扩充； 可以取得数组的全部数据（增加顺序）；  而后继续在这个基础上要求扩展两个派生类：  可以进行数组的排序操作； 可以进行数组的反转处理；  定义一个Array类 在定义">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://assets.qipo.net/%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C.png">
<meta property="og:updated_time" content="2020-06-04T08:30:16.911Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 综合案例：数组操作">
<meta name="twitter:description" content="现在要求定义一个数组（整型数据）的操作类，这个类有如下几点要求：  数组的大小由类创建对象的时候动态决定； 可以通过类向数组中进行数据的保存，保存的时候需要考虑到数组的大小问题； 如果发现数组空间不足，则可以进行数组长度的动态扩充； 可以取得数组的全部数据（增加顺序）；  而后继续在这个基础上要求扩展两个派生类：  可以进行数组的排序操作； 可以进行数组的反转处理；  定义一个Array类 在定义">
<meta name="twitter:image" content="http://assets.qipo.net/%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://blog.qipo.net/2018/02/17/java1/">





  <title>Java 综合案例：数组操作 | Key</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Key</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.qipo.net/2018/02/17/java1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Key">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Key">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Java 综合案例：数组操作</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-17T17:16:54+08:00">
                2018-02-17
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.8k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  8
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>现在要求定义一个数组（整型数据）的操作类，这个类有如下几点要求：</p>
<ul>
<li>数组的大小由类创建对象的时候动态决定；</li>
<li>可以通过类向数组中进行数据的保存，保存的时候需要考虑到数组的大小问题；</li>
<li>如果发现数组空间不足，则可以进行数组长度的动态扩充；</li>
<li>可以取得数组的全部数据（增加顺序）；</li>
</ul>
<p>而后继续在这个基础上要求扩展两个派生类：</p>
<ul>
<li>可以进行数组的排序操作；</li>
<li>可以进行数组的反转处理；</li>
</ul>
<h1 id="定义一个Array类"><a href="#定义一个Array类" class="headerlink" title="定义一个Array类"></a>定义一个Array类</h1><p> 在定义Array类的定义只需要根据自己的需求完成所需要的功能即可，按照标准做。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 数组操作;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.omg.PortableInterceptor.SYSTEM_EXCEPTION;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Array</span></span>&#123;<span class="comment">//定义一个专门进行数组操作的类</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> data [];<span class="comment">//定义一个整型数组，大小由外部决定</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot = <span class="number">0</span>;<span class="comment">//进行数组数据的角标操作</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Array</span><span class="params">(<span class="keyword">int</span> len)</span></span>&#123;<span class="comment">//如果想要使用Array类，就必须定义数组长度</span></span><br><span class="line">        <span class="keyword">if</span>(len &gt; <span class="number">0</span>)&#123;<span class="comment">//一个正常的数组大小</span></span><br><span class="line">            <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="keyword">int</span> [len];<span class="comment">//开辟新数组</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="keyword">int</span> [<span class="number">1</span>];<span class="comment">//开辟一个空间</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//动态扩展，如果此处传入了一个3，则表示在已有的基础上数组长度追加3</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inc</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> newData [] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="keyword">this</span>.data.length+num];</span><br><span class="line">        System.arraycopy(<span class="keyword">this</span>.data,<span class="number">0</span>,newData,<span class="number">0</span>,<span class="keyword">this</span>.data.length);</span><br><span class="line">        <span class="keyword">this</span>.data = newData;<span class="comment">//改变原始数组指向</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//该方法的主要功能是向数组里面进行数组的保存</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.foot &gt;= <span class="keyword">this</span>.data.length)&#123;<span class="comment">//没空间了</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//先进行数组的数据保存，而后foot的内容+1</span></span><br><span class="line">        <span class="keyword">this</span>.data[foot ++] = num;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] getData() &#123;<span class="comment">//取得全部数组数据</span></span><br><span class="line">        <span class="keyword">return</span> data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        Array arr = <span class="keyword">new</span> Array(<span class="number">5</span>);</span><br><span class="line">        System.out.println(arr.add(<span class="number">1</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">2</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">3</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">4</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">5</span>));</span><br><span class="line">        arr.inc(<span class="number">3</span>);</span><br><span class="line">        System.out.println(arr.add(<span class="number">6</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">7</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">8</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; arr.getData().length;x++)&#123;</span><br><span class="line">            System.out.print(arr.getData()[x]+<span class="string">"、"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="http://assets.qipo.net/%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C.png" alt></p>
<h1 id="定义排序扩展类：SortArray"><a href="#定义排序扩展类：SortArray" class="headerlink" title="定义排序扩展类：SortArray"></a>定义排序扩展类：SortArray</h1><p>如果要进行排序的处理操作，那么肯定在取得了全部数据的时候里面的内容应该是排序好了的。同时在该类的操作过程之中应该继续具备有：数据追加，数组扩充，取得全部数据（父类中的getData()方法作为一个获取数据的标准，这个方法应该继续保留）。</p>
<p>范例：定义排序子类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 数组操作;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Array</span></span>&#123;<span class="comment">//定义一个专门进行数组操作的类</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> data [];<span class="comment">//定义一个整型数组，大小由外部决定</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot = <span class="number">0</span>;<span class="comment">//进行数组数据的角标操作</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Array</span><span class="params">(<span class="keyword">int</span> len)</span></span>&#123;<span class="comment">//如果想要使用Array类，就必须定义数组长度</span></span><br><span class="line">        <span class="keyword">if</span>(len &gt; <span class="number">0</span>)&#123;<span class="comment">//一个正常的数组大小</span></span><br><span class="line">            <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="keyword">int</span> [len];<span class="comment">//开辟新数组</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="keyword">int</span> [<span class="number">1</span>];<span class="comment">//开辟一个空间</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//动态扩展，如果此处传入了一个3，则表示在已有的基础上数组长度追加3</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inc</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> newData [] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="keyword">this</span>.data.length+num];</span><br><span class="line">        System.arraycopy(<span class="keyword">this</span>.data,<span class="number">0</span>,newData,<span class="number">0</span>,<span class="keyword">this</span>.data.length);</span><br><span class="line">        <span class="keyword">this</span>.data = newData;<span class="comment">//改变原始数组指向</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//该方法的主要功能是向数组里面进行数组的保存</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.foot &gt;= <span class="keyword">this</span>.data.length)&#123;<span class="comment">//没空间了</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//先进行数组的数据保存，而后foot的内容+1</span></span><br><span class="line">        <span class="keyword">this</span>.data[foot ++] = num;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] getData() &#123;<span class="comment">//取得全部数组数据</span></span><br><span class="line">        <span class="keyword">return</span> data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SortArray</span> <span class="keyword">extends</span> <span class="title">Array</span></span>&#123;</span><br><span class="line">    <span class="comment">//父类之中没有无参构造方法，所以子类必须明确调用父类中的构造方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">SortArray</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(num); <span class="comment">//父类中已经支持数组创建</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//父类中的取得数据的方法名称很标准，但是功能不足，又希望继续使用这个方法名称，那么就需要对方法进行扩充，扩充就是方法覆写的核心作用</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] getData() &#123;</span><br><span class="line">        Java.util.Arrays.sort(<span class="keyword">super</span>.getData());</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">super</span>.getData();<span class="comment">//引用传递</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        SortArray arr = <span class="keyword">new</span> SortArray(<span class="number">5</span>);</span><br><span class="line">        System.out.println(arr.add(<span class="number">4</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">5</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">3</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">2</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">7</span>));</span><br><span class="line">        arr.inc(<span class="number">3</span>);</span><br><span class="line">        System.out.println(arr.add(<span class="number">8</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">3</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">2</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; arr.getData().length;x++)&#123;</span><br><span class="line">            System.out.print(arr.getData()[x]+<span class="string">"、"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>不同的子类可能要针对于方法进行部分扩展。</p>
<h1 id="定义反转子类：ReverseArray"><a href="#定义反转子类：ReverseArray" class="headerlink" title="定义反转子类：ReverseArray"></a>定义反转子类：ReverseArray</h1><p>反转子类的最大特点在于，取得的数据顺序的保存顺序的相反内容。整体的实现风格实际上是和排序的数据子类是一样的。</p>
<p>范例：定义反转子类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 数组操作;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Array</span></span>&#123;<span class="comment">//定义一个专门进行数组操作的类</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> data [];<span class="comment">//定义一个整型数组，大小由外部决定</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot = <span class="number">0</span>;<span class="comment">//进行数组数据的角标操作</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Array</span><span class="params">(<span class="keyword">int</span> len)</span></span>&#123;<span class="comment">//如果想要使用Array类，就必须定义数组长度</span></span><br><span class="line">        <span class="keyword">if</span>(len &gt; <span class="number">0</span>)&#123;<span class="comment">//一个正常的数组大小</span></span><br><span class="line">            <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="keyword">int</span> [len];<span class="comment">//开辟新数组</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="keyword">int</span> [<span class="number">1</span>];<span class="comment">//开辟一个空间</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//动态扩展，如果此处传入了一个3，则表示在已有的基础上数组长度追加3</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inc</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> newData [] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="keyword">this</span>.data.length+num];</span><br><span class="line">        System.arraycopy(<span class="keyword">this</span>.data,<span class="number">0</span>,newData,<span class="number">0</span>,<span class="keyword">this</span>.data.length);</span><br><span class="line">        <span class="keyword">this</span>.data = newData;<span class="comment">//改变原始数组指向</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//该方法的主要功能是向数组里面进行数组的保存</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.foot &gt;= <span class="keyword">this</span>.data.length)&#123;<span class="comment">//没空间了</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//先进行数组的数据保存，而后foot的内容+1</span></span><br><span class="line">        <span class="keyword">this</span>.data[foot ++] = num;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] getData() &#123;<span class="comment">//取得全部数组数据</span></span><br><span class="line">        <span class="keyword">return</span> data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SortArray</span> <span class="keyword">extends</span> <span class="title">Array</span></span>&#123;</span><br><span class="line">    <span class="comment">//父类之中没有无参构造方法，所以子类必须明确调用父类中的构造方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">SortArray</span><span class="params">(<span class="keyword">int</span> len)</span></span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(len); <span class="comment">//父类中已经支持数组创建</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//父类中的取得数据的方法名称很标准，但是功能不足，又希望继续使用这个方法名称，那么就需要对方法进行扩充，扩充就是方法覆写的核心作用</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] getData() &#123;</span><br><span class="line">        Java.util.Arrays.sort(<span class="keyword">super</span>.getData());</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">super</span>.getData();<span class="comment">//引用传递</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ReverseArray</span> <span class="keyword">extends</span> <span class="title">Array</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ReverseArray</span><span class="params">(<span class="keyword">int</span> len)</span></span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(len);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] getData()&#123;</span><br><span class="line">        <span class="keyword">int</span> center = <span class="keyword">super</span>.getData().length / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> head = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> tail = <span class="keyword">super</span>.getData().length - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt;center;x++)&#123;</span><br><span class="line">            <span class="keyword">int</span> temp = <span class="keyword">super</span>.getData()[head];</span><br><span class="line">            <span class="keyword">super</span>.getData()[head] = <span class="keyword">super</span>.getData()[tail];</span><br><span class="line">            <span class="keyword">super</span>.getData()[tail] = temp;</span><br><span class="line">            head++;</span><br><span class="line">            tail--;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">super</span>.getData();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        ReverseArray arr = <span class="keyword">new</span> ReverseArray(<span class="number">5</span>);</span><br><span class="line">        System.out.println(arr.add(<span class="number">4</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">5</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">3</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">2</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">7</span>));</span><br><span class="line">        arr.inc(<span class="number">3</span>);</span><br><span class="line">        System.out.println(arr.add(<span class="number">8</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">3</span>));</span><br><span class="line">        System.out.println(arr.add(<span class="number">2</span>));</span><br><span class="line">        <span class="keyword">int</span> result[] = arr.getData();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>;x &lt; result.length;x++)&#123;</span><br><span class="line">            System.out.print(result[x]+<span class="string">"、"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>本程序很完美的表现出了对继承概念讲解以及覆写的核心意义。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Key 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Key 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Java/" rel="tag"># Java</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/02/17/java3/" rel="next" title="Java 多态性">
                <i class="fa fa-chevron-left"></i> Java 多态性
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/03/08/java/" rel="prev" title="Java 抽象类的定义以及使用">
                Java 抽象类的定义以及使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Key">
            
              <p class="site-author-name" itemprop="name">Key</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">107</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/keyarea" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:wxkayle@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#定义一个Array类"><span class="nav-number">1.</span> <span class="nav-text">定义一个Array类</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#定义排序扩展类：SortArray"><span class="nav-number">2.</span> <span class="nav-text">定义排序扩展类：SortArray</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#定义反转子类：ReverseArray"><span class="nav-number">3.</span> <span class="nav-text">定义反转子类：ReverseArray</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-snowflake-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Key</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">190.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
